package sensor;

import houseHold.House;
import houseHold.Room;

import java.util.Iterator;
import java.util.Set;

import person.PersonAchive;
import RoomPanel.IDGenerator;
import constants.Constants;
import consumption.ConsumptionType;
import done.Information;
import done.ScaleWriter;

public class Sensor {
	private double sensorData;
	private int sensorID;
	private String sensorName;
	private consumption.ConsumptionType sensorRecord;
	private SensorType type;

	public Sensor() {
		sensorID = IDGenerator.getNextSensorID();
		sensorName = "Sensor#" + sensorID;
		IDGenerator.incrementSensorID();
		hasWriter=false;
	}

	public Sensor(int id) {
		sensorID = id;
		sensorName = "Sensor#" + id;
	}

	private void collectData(double sensorData2) {
		sensorData += sensorData2;
	}

	private void getConsumptionData(Room cRoom) {
		resetData();
		Set<String> ks = cRoom.getItemList().keySet();
		Iterator<String> it = ks.iterator();
		while (it.hasNext()) {
			String curr = it.next();
			if (cRoom.getItem(curr).getSensor().equals(this))
				if (cRoom.getItem(curr).isActive()) {
					cRoom.getItem(curr).addPastActivity(Constants.time);
					if (cRoom.getItem(curr).getActiveDuration() > 0) {
						cRoom.getItem(curr).decrementActiveDuration(
								Constants.time);
					}
					collectData(cRoom.getItem(curr).getCurrentConsumption());
				}
		}
	}

	private void getLightData(Room r, int time) {
		collectData(LumenCalculator.compute(r, time));
	}

	private void getPresenceData(Room cRoom, int t) {
		if (t > 30)
			for (int i = 0; i < PersonAchive.getCount(); i++) {
				System.out.println(PersonAchive.getPerson(i).isInHouse());
				if (PersonAchive.getPerson(i).isInHouse()){
				if (PersonAchive.getPerson(i).getCurrentLocation().getCurrent()
						.equalsIgnoreCase(cRoom.getName()))
					collectData(1);
				}
				else collectData(0);
			}
	}

	private void getRFidData() {
		for (int i = 0; i < PersonAchive.getCount(); i++)
			if (PersonAchive.getPerson(i).isInHouse()){
			if (PersonAchive.getPerson(i).getrFid().equals(this))
			{
				collectData(House.getInstance().getRoom(PersonAchive.getPerson(i).getCurrentLocation().getCurrent()).getID());
				break;
			}
			}else 
				if (PersonAchive.getPerson(i).getrFid().equals(this)){
				collectData((-1));
				break;
			}
			
	}

	public double getSensorData(Room cRoom, int time) {
		resetData();
		switch (this.type) {
		case WINDOW_SENSOR:
			getWindowData(cRoom);
			break;
		case GAS_SENSOR:
			getConsumptionData(cRoom);
			break;
		case WATER_SENSOR:
			getConsumptionData(cRoom);
			break;
		case ELECTRIC_SENSOR:
			getConsumptionData(cRoom);
			break;
		case RF_SENSOR:
			getRFidData();
			break;
		case TEMPERATURE_SENSOR:
			getTemperatureData(cRoom, time);
			break;
		case LIGHT_SENSOR:
			getLightData(cRoom, time);
			break;
		case PR_SENSOR:
			getPresenceData(cRoom, time);
			break;
		}
		if(hasWriter)
			sw.writeNext(new Information(sensorData, time));
		return sensorData;
	}

	public int getSensorID() {
		return sensorID;
	}

	public String getSensorName() {
		return sensorName;
	}

	public consumption.ConsumptionType getSensorRecord() {
		return sensorRecord;
	}

	private void getTemperatureData(Room cRoom, int time) {
		double rs = TemperatureCalculator.compute(cRoom, time);
		collectData(rs);
	}

	public SensorType getType() {
		return type;
	}

	private void getWindowData(Room cRoom) {
		if (cRoom.isWindowStatus())
			collectData(1);
		else
			collectData(0);

	}

	public String print() {
		String result = new String();
		result = result.concat("\nSensor:\n");
		result = result.concat("Name= " + sensorName);
		result = result.concat("\nType= " + type);
		result = result.concat("\n");
		return result;
	}

	public void resetData() {
		sensorData = 0;
	}

	public void setSensorData(int sensorData) {
		this.sensorData = sensorData;
	}

	public void setSensorID(int sensorID) {
		this.sensorID = sensorID;
	}

	public void setSensorName(String sensorName) {
		this.sensorName = sensorName;
	}

	public void setType(SensorType type) {
		switch (type) {
		case GAS_SENSOR:
			this.sensorRecord = ConsumptionType.GAS;
			break;
		case WATER_SENSOR:
			this.sensorRecord = ConsumptionType.WATER;
			break;
		case ELECTRIC_SENSOR:
			this.sensorRecord = ConsumptionType.ELECTRICITY;
			break;
		}
		this.sensorName = type.toString() + "#" + sensorID;
		this.type = type;
	}

	@Override
	public String toString() {
		return this.sensorName;
	}
	private ScaleWriter sw=null;
	private boolean hasWriter=false;
	public void addScaleWriter(ScaleWriter w)
	{
		sw=w;
		hasWriter=true;
	}

}
